﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
namespace Caifu.Fx
{
	/// <summary>
	/// MD5加密类
	/// </summary>
	public class MD5Hash
	{

		/// <summary>
		/// MD5加密
		/// </summary>
		/// <param name="input">源字符串</param>
		/// <returns>返回加密的字符串(32位小写字母)</returns>
		public static String ComputeHash(String input)
		{
			MD5 MD5Hasher = MD5.Create();
			byte[] data = MD5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
			MD5Hasher.Clear();
			StringBuilder sBuilder = new StringBuilder();
			for (int i = 0; i < data.Length; i++)
			{
				sBuilder.Append(data[i].ToString("x2"));
			}
			return sBuilder.ToString();
		}

        /// <summary>
        /// 比较
        /// </summary>
        /// <param name="source">源字符串（由用户输入）</param>
        /// <param name="target">目标字符串（数据库中）</param>
        /// <returns></returns>
        public static Boolean Compare(String source,String target)
        {
            return ComputeHash(source).Equals(target);
        }
	}
}
